package com.jiutong.meipenggang.ui.fragments;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.app.hubert.library.HighLight;
import com.app.hubert.library.NewbieGuide;
import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.BaseFragment;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.MainPageBean;
import com.jiutong.meipenggang.http.model.interfaces.MainPageBiz;
import com.jiutong.meipenggang.http.present.MainPagePresent;
import com.jiutong.meipenggang.ui.activitys.AvailableBalanceActivity;
import com.jiutong.meipenggang.ui.activitys.HongbaoActivity;
import com.jiutong.meipenggang.ui.activitys.MyInverstActivity;
import com.jiutong.meipenggang.ui.activitys.RechargeActivity;
import com.jiutong.meipenggang.ui.activitys.RegisterActivity;
import com.jiutong.meipenggang.ui.activitys.TotalMoneyActivity;
import com.jiutong.meipenggang.ui.activitys.WithdrawActivity;
import com.jiutong.meipenggang.utils.SpConfig;
import com.orhanobut.logger.Logger;

import butterknife.Bind;
import butterknife.OnClick;

import static com.jiutong.meipenggang.R.id.view;

/**
 * Created by suxi on 2017/5/24.
 */

public class HomeFragment extends BaseFragment implements MainPageBiz {

    @Bind(R.id.tv_login)
    TextView tvLogin;
    @Bind(R.id.tv_registe)
    TextView tvRegiste;
    @Bind(R.id.tv_usage_balance)
    LinearLayout tvUsageBalance;
    @Bind(R.id.tv_total_earn)
    TextView tvTotalEarn;
    @Bind(R.id.tv_yestoday_earn)
    TextView tvYestodayEarn;
    @Bind(R.id.tv_tixian)
    TextView tvTixian;
    @Bind(R.id.tv_chongzhi)
    TextView tvChongzhi;
    @Bind(R.id.tv_total_earn_num)
    TextView tvTotalEarnNum;
    @Bind(R.id.imageView2)
    ImageView adPic;
    @Bind(R.id.iv_cicle)
    ImageView ivCicle;
    @Bind(R.id.imageView3)
    ImageView imageView3;
    @Bind(R.id.imageView4)
    ImageView imageView4;
    @Bind(R.id.ll_home_hongbao)
    LinearLayout llHomeHongbao;
    @Bind(R.id.imageView5)
    ImageView imageView5;
    @Bind(R.id.textView3)
    TextView textView3;
    @Bind(view)
    View shadow;
    @Bind(R.id.sv_container)
    ScrollView svContainer;
    @Bind(R.id.tv_yestoday_earn_num)
    TextView tvYestodayEarnNum;
    @Bind(R.id.tv_total_assert)
    TextView tvTotalAssert;
    @Bind(R.id.tv_balance)
    TextView tvBalance;
    @Bind(R.id.tv_mxb_ll)
    TextView tvMxbLl;
    @Bind(R.id.tv_mytz_ll)
    TextView tvMytzLl;
    @Bind(R.id.rl_dlh)
    RelativeLayout rlDlh;
    @Bind(R.id.ll_tx_cz)
    LinearLayout llTxCz;
    @Bind(R.id.ll_dl_zc)
    LinearLayout llDlZc;
    @Bind(R.id.ll_total_assert)
    LinearLayout llTotalAssert;
    @Bind(R.id.ll_my_inverst)
    LinearLayout llMyInverst;

    private MainPagePresent mainPagePresent;
    private boolean isLogin;
    private boolean mHidden = false;

    @Override
    protected int getLayoutResouceId() {
        return R.layout.fragment_home;
    }

    private void LoginYet() {
        isLogin = SpConfig.getInstance().getBool(Constants.IS_LOGIN);
        if (isLogin) {
            shadow.setVisibility(View.GONE);
            adPic.setVisibility(View.GONE);
            rlDlh.setVisibility(View.VISIBLE);
            llDlZc.setVisibility(View.GONE);
            llTxCz.setVisibility(View.VISIBLE);
            mainPagePresent = new MainPagePresent(getContext(), this);
            mainPagePresent.MainPage();
        }else{
            shadow.setVisibility(View.VISIBLE);
            adPic.setVisibility(View.VISIBLE);
            rlDlh.setVisibility(View.GONE);
            llDlZc.setVisibility(View.VISIBLE);
            llTxCz.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initView() {
        super.initView();
        NewbieGuide.with(getActivity())
                .setLabel("guide1")
                .addHighLight(tvRegiste, HighLight.Type.OVAL)
                .setLayoutRes(R.layout.view_guide)
                .show();
        LoginYet();
    }

    @Override
    public void onResume() {
        super.onResume();
        isLogin = SpConfig.getInstance().getBool(Constants.IS_LOGIN);
        if (mHidden)
            return;
        LoginYet();
    }

    @OnClick({R.id.tv_login, R.id.tv_registe, R.id.tv_usage_balance, R.id.tv_tixian, R.id.tv_chongzhi, R.id.ll_home_hongbao, R.id.ll_total_assert, R.id.ll_my_inverst})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                isLogin();
                break;
            case R.id.tv_registe:
                openActivity(RegisterActivity.class);
                break;
            case R.id.tv_usage_balance:
                if (isLogin) {
                    startActivity(new Intent(getContext(), AvailableBalanceActivity.class));
                }
                break;
            case R.id.tv_tixian:
                startActivity(new Intent(getContext(), WithdrawActivity.class));
                break;
            case R.id.tv_chongzhi:
                startActivity(new Intent(getContext(), RechargeActivity.class));
                break;
            case R.id.ll_home_hongbao:
                if (isLogin) {
                    openActivity(HongbaoActivity.class);
                }
                break;
            case R.id.ll_total_assert:
                if (isLogin) {
                    openActivity(TotalMoneyActivity.class);
                }
                break;
            case R.id.ll_my_inverst:
                openActivity(MyInverstActivity.class);
                break;

        }
    }


    @Override
    public String getPersonalId() {
        return null;
    }

    @Override
    public String getToken() {
        return null;
    }

    @Override
    public void onMainPageSuccess(MainPageBean subjects) {

    }

    @Override
    public void onSuccess(Object object) {
        MainPageBean respone = (MainPageBean) object;
        String totalNum = respone.getTotalIncome();
        tvTotalEarnNum.setText(totalNum + "");
        tvYestodayEarnNum.setText(respone.getYesterdayIncome() + "");
        tvTotalAssert.setText(respone.getTotalAssets() + "");
        tvBalance.setText(respone.getBalance() + "");
        tvMxbLl.setText(respone.getMeiMin() + "%~" + respone.getMeiMax() + "%");
        tvMytzLl.setText(respone.getInvestMin() + "%~" + respone.getInvestMax() + "%");
        SpConfig.getInstance().edit().putString(Constants.ID, respone.getId().toString()).commit();
        SpConfig.getInstance().edit().putString(Constants.TOKEN, respone.getToken()).commit();
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Logger.d("onHiddenChanged",hidden+"");
        mHidden = hidden;
        if (!hidden) {
            LoginYet();
        }
    }

}

